package cn.net.xinyi.xs.service.hr.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.net.xinyi.xs.dao.hr.OrganDao;
import cn.net.xinyi.xs.model.hr.Organization;
import cn.net.xinyi.xs.model.vo.PageVO;
import cn.net.xinyi.xs.service.hr.OrganManager;
import cn.net.xinyi.xs.service.impl.BaseManager;
import cn.net.xinyi.xs.util.CustomComparator;
import cn.net.xinyi.xs.util.PropertyUtil;

public class OrganManagerImpl extends BaseManager implements OrganManager {
	private OrganDao dao;

	public void setOrganDao(OrganDao dao) {
		this.dao = dao;
	}

	public Organization getOrgan(Long id) {
		return dao.getOrgan(id);
	}

	public Organization getOrganWithChild(Long id) {
		return dao.getOrganWithChild(id);
	}

	public List<Organization> getOrgans(PageVO pageVO) {
		return (List<Organization>) dao.getOrgans(pageVO.getStartIndex(),
				pageVO.getPageSize());
	}

	public int getOrganCount() {
		return dao.getOrganCount();
	}

	public List<Organization> getOrganChildren(Long id) {
		return dao.getOrganChildren(id);
	}

	public List<Organization> getOrganRecursionChild(Long id) {
		return dao.getOrganRecursionChild(id);
	}

	@SuppressWarnings("unchecked")
	public List<Organization> getAllBranchOrgans() {
		List<Organization> organs = dao.getAllBranchOrgans(new HashMap());
		List<Organization> resultList = new ArrayList<Organization>();
		for (Organization organ : organs) {
			String sort = PropertyUtil.getSystemText(organ.getName());
			if (!"".equals(sort)) {
				organ.setSort(Integer.parseInt(sort));
				organ.setName(organ.getName().replaceAll("\u516c\u5b89", ""));
				resultList.add(organ);
			}
		}
		Collections.sort(resultList, new CustomComparator());
		return resultList;
	}

	@SuppressWarnings("unchecked")
	public List<Organization> getBranchOrganByOrgCode(long organId) {
		Map map = new HashMap();
		Organization org = dao.getOrgan(organId);
		String code = org.getCode().substring(0, 6);
		map.put("code", code);
		return (List<Organization>) dao.getBranchOrganByOrgCode(map);
	}
}
